package com.vakitdaraliyor.www.dao;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import com.vakitdaraliyor.www.entity.User;

@Stateless
public class UserDao {

	@PersistenceContext
	private EntityManager entityManager;

	public UserDao() {
		super();
	}


	public User registerUser(User user) {
		entityManager.persist(user);
		return user;
	}

	public User kullaniciGetirById(Integer id) {
		return (User) entityManager
				.createQuery("select a from users a where a.id =:id")
				.setParameter("id", id).getSingleResult();
	}

	public User kullaniciGetirByKullaniciAdi(String username) {

		try {
			return (User) entityManager
					.createQuery("select i from User i where i.username =:username")
					.setParameter("username", username)
					.getSingleResult();
		} catch (Exception e) {
			System.err.println(e.getMessage());
			return null;
		}
	}

}
